/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.BankDAO;
import com.ivanbiz.dao.impl.BankDAOImpl;
import com.ivanbiz.model.Bank;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class BankActionService extends Window {

    BankDAO bankDAO = new BankDAOImpl();
    Textbox nama;
    Textbox deskripsi;

    public void onCreate() {
        nama = (Textbox) getFellow("nama");
        deskripsi = (Textbox) getFellow("deskripsi");
    }

    public void simpan() {
        Bank bank = (Bank) getAttribute("bank");
        if (bank == null) {
            bank = new Bank();
        }
        bank.setNama(nama.getValue());
        bank.setDeskripsi(deskripsi.getValue());
        validate(bank);
    }

    private void validate(Bank bank) {
        if (bank.getNama().isEmpty()) {
            Messagebox.show("Nama tidak boleh kosong");
        } else if (bank.getDeskripsi().isEmpty()) {
            Messagebox.show("Deskripsi tidak boleh kosong");
        } else {
            try {
                bankDAO.saveOrUpdate(bank);
                detach();
            } catch (Exception ex) {
                Logger.getLogger(BankService1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
}
